package net.huashitong.Indicators.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.sirdc.modules.utils.date.DateUtils;

public class DateForIndUtils {
    /**
     * 获取n个月后的时间
     *
     * @return yyyyMMdd
     * @throws ParseException
     */
    public static String getNextMonthDate(String strDate, int n) throws ParseException {
        Date date = new SimpleDateFormat("yyyyMM").parse(strDate);

        Calendar calendar = Calendar.getInstance();//创建实例

        calendar.setTime(date);

        calendar.add(Calendar.MONTH, n);

        date = calendar.getTime();

        String curdate = DateUtils.formatDate(date, "yyyyMM");


        return curdate;
    }

    /**
     * 获取几年后的时间
     *
     * @return yyyyMMdd
     * @throws ParseException
     */
    public static String getNextYearDate(String strDate, int n) throws ParseException {
        Date date = new SimpleDateFormat("yyyy").parse(strDate);

        Calendar calendar = Calendar.getInstance();//创建实例

        calendar.setTime(date);

        calendar.add(Calendar.YEAR, n);

        date = calendar.getTime();

        String curdate = DateUtils.formatDate(date, "yyyy");


        return curdate;
    }

    /**
     * 获取n个月后的时间
     *
     * @return yyyyMMdd
     * @throws ParseException
     */
    public static String getNextMonthValueDate(String strDate, int n) throws ParseException {
        Date date = new SimpleDateFormat("yyyyMM-MM").parse(strDate);

        Calendar calendar = Calendar.getInstance();//创建实例

        calendar.setTime(date);

        calendar.add(Calendar.MONTH, n);

        date = calendar.getTime();

        String curdate = DateUtils.formatDate(date, "yyyyMM-MM");

        curdate = curdate.substring(0, 4) + "01" + curdate.substring(6);
        return curdate;
    }

    /**
     * @param date
     * @return
     */
    public static String getMonth(String date) {
        if (date.contains("01-")) {
            return date;
        }
        date = date.substring(0, 4) + "01-" + date.substring(4);
        return date;
    }

    /**
     * 获取当前日期
     *
     * @param format
     * @return
     */
    public static String getCurDate(String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        String date = df.format(new Date());
        return date;
    }

    public static String getCMonth(String date) {
        if (date.contains("01-")) {
            return date.replace("01-", "");
        }

        return date;
    }

    /**
     * 6      * 时间戳转换成日期格式字符串
     * 7      * @param seconds 精确到秒的字符串
     * 8      * @param formatStr
     * 9      * @return
     * 10
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds + "000")));
    }


    /**
     * 字符串转化为标准时间
     *
     * @param text
     * @param formate
     * @return
     */
    public static Date toDate(String text, String... formate) {
        SimpleDateFormat sf = new SimpleDateFormat(formate[0].toString());
        try {
            return sf.parse(text);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * yyyy-MM-dd HH:mm:ss  ->  获取long型时间，单位毫秒
     * @param inVal
     * @return
     */
    public static long fromDateStringToLong(String inVal) {
        Date date = null;
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        try {
            date = inputFormat.parse(inVal);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date.getTime();
    }
}
